package annuaire.beans;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PostUpdate;
import javax.persistence.PreUpdate;
import javax.persistence.Transient;
import javax.persistence.Version;

/**
 * Bean représentant un étudiant.
 * 
 * @author Yannick Lorenzati & Stephanie Malakian
 * 
 */
@Entity(name = "Student")
public class Student implements Serializable, Comparable<Student>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2851209095540879867L;

	@Id()
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;

	@Basic(optional = false)
	@Column(name = "login", length = 200, nullable = false, unique = true)
	private String login;

	@Basic(optional = false)
	@Column(name = "name", length = 200, nullable = false, unique = false)
	private String name;

	@Basic()
	@Column(name = "firstName", length = 200, nullable = true, unique = false)
	private String firstName;

	@Basic(optional = false)
	@Column(name = "pwd", length = 200, nullable = false, unique = false)
	private String pwd;

	@Basic
	@Column(name = "address", length = 200, nullable = true, unique = false)
	private String address;

	@Basic()
	@Column(name = "mail", length = 200, nullable = true, unique = false)
	private String mail;

	@Basic()
	@Column(name = "web", length = 200, nullable = true, unique = false)
	private String web;

	@Basic()
	@Column(name = "society", length = 200, nullable = true, unique = false)
	private String society;

	@Basic()
	@Column(name = "comments", length = 300, nullable = true, unique = false)
	private String comments;

	@Basic()
	@Column(name = "lastUpdate", length = 200, nullable = true, unique = false)
	private Date lastUpdate;

	@Version
	private long version = 0;

	@ManyToOne(optional = true)
	@JoinColumn(name = "promo")
	private Promotion promo;

	@Transient
	public static long updateCounter = 0;

	public Student() {
	}

	public Student(String login, String pwd, String name) {
		super();
		this.login = login;
		this.pwd = pwd;
		this.name = name;
	}

	@PreUpdate
	public void beforeUpdate() {
		System.err.println("PreUpdate of " + this);
	}

	@PostUpdate
	public void afterUpdate() {
		System.err.println("PostUpdate of " + this);
		Student.updateCounter++;
	}

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("\nName : " + firstName + " " + name + "\n");
		buffer.append("Address: " + address + "\n");
		buffer.append("Web: " + web + "\n");
		buffer.append("Society: " + society + "\n");
		buffer.append("Comments: " + comments + "\n");
		buffer.append("Last update: " + lastUpdate + "\n");
		return buffer.toString();
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param login
	 *            the login to set
	 */
	public void setLogin(String login) {
		this.login = login;
	}

	/**
	 * @return the login
	 */
	public String getLogin() {
		return login;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the firstName
	 */
	public String getFirstName() {
		return firstName;
	}

	/**
	 * @param firstName
	 *            the firstName to set
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	/**
	 * @return the pwd
	 */
	public String getPwd() {
		return pwd;
	}

	/**
	 * @param pwd
	 *            the pwd to set
	 */
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	/**
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}

	/**
	 * @param address
	 *            the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}

	/**
	 * @return the mail
	 */
	public String getMail() {
		return mail;
	}

	/**
	 * @param mail
	 *            the mail to set
	 */
	public void setMail(String mail) {
		this.mail = mail;
	}

	/**
	 * @return the web
	 */
	public String getWeb() {
		return web;
	}

	/**
	 * @param web
	 *            the web to set
	 */
	public void setWeb(String web) {
		this.web = web;
	}

	/**
	 * @return the society
	 */
	public String getSociety() {
		return society;
	}

	/**
	 * @param society
	 *            the society to set
	 */
	public void setSociety(String society) {
		this.society = society;
	}

	/**
	 * @return the comments
	 */
	public String getComments() {
		return comments;
	}

	/**
	 * @param comments
	 *            the comments to set
	 */
	public void setComments(String comments) {
		this.comments = comments;
	}

	/**
	 * @return the lastUpdate
	 */
	public Date getLastUpdate() {
		return lastUpdate;
	}

	/**
	 * @param lastUpdate
	 *            the lastUpdate to set
	 */
	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
	}

	/**
	 * @param promo
	 *            the promo to set
	 */
	public void setPromo(Promotion promo) {
		this.promo = promo;
	}

	/**
	 * @return the promo
	 */
	public Promotion getPromo() {
		return promo;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setVersion(long version) {
		this.version = version;
	}

	public long getVersion() {
		return version;
	}
	

	@Override
	public int compareTo(Student s2) {
		if( this.name.compareTo(s2.getName()) < 0 ){
            return -1;
        }
        else if( this.name.compareTo(s2.getName()) == 0 ){
            return 0;
        }
        else{
            return 1;
        }
	}

}
